package com.yztc.thinkpad.mandemo;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import fragments.FragmentCool;
import fragments.FragmentHome;
import fragments.FragmentMatch;
import fragments.FragmentMine;
import fragments.FragmentSingle;

//主布局
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.main_layout)
    FrameLayout mainLayout;
    @Bind(R.id.rb_home_btn)
    RadioButton rbHomeBtn;
    @Bind(R.id.rb_single_btn)
    RadioButton rbSingleBtn;
    @Bind(R.id.rb_match_btn)
    RadioButton rbMatchBtn;
    @Bind(R.id.rb_cool_btn)
    RadioButton rbCoolBtn;
    @Bind(R.id.rb_mine_btn)
    RadioButton rbMineBtn;
    @Bind(R.id.rg_main)
    RadioGroup rgMain;
    @Bind(R.id.iv_user)
    ImageView ivUser;
    @Bind(R.id.age_15)
    RelativeLayout age15;
    @Bind(R.id.age_19)
    RelativeLayout age19;
    @Bind(R.id.age_24)
    RelativeLayout age24;
    @Bind(R.id.age_29)
    RelativeLayout age29;
    @Bind(R.id.age_36)
    RelativeLayout age36;
    @Bind(R.id.drawer_left)
    LinearLayout drawerLeft;
    @Bind(R.id.drawerlayout)
    DrawerLayout drawerlayout;
    private FragmentManager manager;
    //初始化fragment
    private FragmentCool fragmentCool;
    private FragmentHome fragmentHome;
    private FragmentSingle fragmentSingle;
    private FragmentMatch fragmentMatch;
    private FragmentMine fragmentMine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initview();
        initdata();
    }

    private void initdata() {
        Setselection(0);
    }

    private void initview() {
        manager = getSupportFragmentManager();
        rgMain.setOnCheckedChangeListener(this);
        //打开左侧抽屉只显示3/4
        ViewGroup.LayoutParams layoutParams = drawerLeft.getLayoutParams();
        layoutParams.width = getResources().getDisplayMetrics().widthPixels * 3 / 4;
        drawerLeft.setLayoutParams(layoutParams);
    }

    private void Setselection(int index) {
        //FragmentTransaction对fragment进行添加，删除，替换，等其他执行操作
        FragmentTransaction ft = manager.beginTransaction();
        hindFragment(ft);
        //需要加载的页面
        switch (index) {
            case 0://添加第一个homeFragment
                if (fragmentHome == null) {
                    fragmentHome = new FragmentHome();
                    ft.add(R.id.main_layout, fragmentHome);
                } else {
                    ft.show(fragmentHome);
                }
                break;
            case 1://添加第二个singleFragment
                if (fragmentSingle == null) {
                    fragmentSingle = new FragmentSingle();
                    ft.add(R.id.main_layout, fragmentSingle);
                } else {
                    ft.show(fragmentSingle);
                }
                break;
            case 2://添加第三个matchFragment
                if (fragmentMatch == null) {
                    fragmentMatch = new FragmentMatch();
                    ft.add(R.id.main_layout, fragmentMatch);
                } else {
                    ft.show(fragmentMatch);
                }
                break;
            case 3://第四个coolFragment
                if (fragmentCool == null) {
                    fragmentCool = new FragmentCool();
                    ft.add(R.id.main_layout, fragmentCool);
                } else {
                    ft.show(fragmentCool);
                }
                break;
            case 4://添加mineFragemnt
                if (fragmentMine == null) {
                    fragmentMine = new FragmentMine();
                    ft.add(R.id.main_layout, fragmentMine);
                } else {
                    ft.show(fragmentMine);
                }
                break;
        }
        ft.commit();
    }

    private void hindFragment(FragmentTransaction ft) {
        //如果fragment不为空则保存当前fragment状态
        if (fragmentHome != null) {
            ft.hide(fragmentHome);
        }
        if (fragmentSingle != null) {
            ft.hide(fragmentSingle);
        }
        if (fragmentMatch != null) {
            ft.hide(fragmentMatch);
        }
        if (fragmentCool != null) {
            ft.hide(fragmentCool);
        }
        if (fragmentMine != null) {
            ft.hide(fragmentMine);
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            //首页
            case R.id.rb_home_btn:
                Setselection(0);
                break;
            //单品
            case R.id.rb_single_btn:
                Setselection(1);
                break;
            //搭配
            case R.id.rb_match_btn:
                Setselection(2);
                break;
            //酷吧
            case R.id.rb_cool_btn:
                Setselection(3);
                break;
            //我的
            case R.id.rb_mine_btn:
                Setselection(4);
                break;
        }
    }
    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }
}
